package com.cincc.cinccimdemo.viewholder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.adapter.ConversationAdapter;
import com.cincc.cinccimdemo.model.MessageInfoUI;
import com.cincc.im.callback.ImCallback;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.FileAttrsInfo;
import com.cincc.im.model.ImResponse;
import com.cincc.im.model.enums.ImSendType;
import com.cincc.im.model.message.ImAudioMessage;
import com.cincc.im.util.ImManager;
import com.google.android.flexbox.FlexboxLayout;

public class ReceiveAudioHolder extends MessageContentHolder {
    public ImageView iv_face;
    public ImageView iv_message;
    public TextView tv_duration;
    public FlexboxLayout fl_message;
    public TextView tv_time;
    public ImageView iv_unreadDot;
    public TextView tv_username;
    public RelativeLayout rl_message;

    public ReceiveAudioHolder(Context context, View itemView, ConversationAdapter adapter) {
        super(context, itemView, adapter);
        iv_face = itemView.findViewById(R.id.iv_face);
        iv_message = itemView.findViewById(R.id.iv_message);
        tv_duration = itemView.findViewById(R.id.tv_duration);
        fl_message = itemView.findViewById(R.id.fl_message);
        tv_time = itemView.findViewById(R.id.tv_time);
        iv_unreadDot = itemView.findViewById(R.id.iv_unreadDot);
        tv_username = itemView.findViewById(R.id.username_text);
        rl_message = itemView.findViewById(R.id.rl_message);
    }

    @Override
    public void bindView(MessageInfoUI uiMessage, int position) {
        super.bindView(uiMessage, position);
        ImAudioMessage imAudioMessage = (ImAudioMessage) uiMessage.getMessageInfo().imMessage;

        tv_duration.setText(imAudioMessage.duration + "\"");
        iv_unreadDot.setVisibility(uiMessage.isPlayed() ? View.GONE : View.VISIBLE);
        setAudioWidth(imAudioMessage.duration, rl_message, context);
        setAudioDrawable(uiMessage, iv_message, false);
        rl_message.setOnClickListener(v -> {
            IMSipPhoneCtrl.getInstance().setVoicePlayed(uiMessage.getMessageInfo().imSendType,
                    ImSendType.isP2P(uiMessage.getMessageInfo().imSendType) ? uiMessage.getMessageInfo().senderId : uiMessage.getMessageInfo().groupId,
                    uiMessage.getMessageInfo().messageId);
            if (!uiMessage.getMessageInfo().isSent && !uiMessage.isPlayed()) {
                FileAttrsInfo fileAttrsInfo = IMSipPhoneCtrl.getInstance().getFileAttrsInfo(uiMessage.getMessageInfo().imSendType,
                        ImSendType.isP2P(uiMessage.getMessageInfo().imSendType) ? uiMessage.getMessageInfo().senderId : uiMessage.getMessageInfo().groupId,
                        uiMessage.getMessageInfo().messageId);

                uiMessage.setPlayed(fileAttrsInfo.played);
            }
            if (TextUtils.isEmpty(uiMessage.getMessageInfo().localPath)) {
                downloadAudio(uiMessage);
            } else {
                conversationViewModel.playAudioMessage(uiMessage);
            }
        });
        tv_username.setText(uiMessage.getMessageInfo().senderName);
        tv_username.setVisibility(ImSendType.isP2P(uiMessage.getMessageInfo().imSendType) ? View.GONE : View.VISIBLE);
    }

    public void downloadAudio(MessageInfoUI item) {
        ImAudioMessage imAudioMessage = (ImAudioMessage) item.getMessageInfo().imMessage;
        IMSipPhoneCtrl.getInstance().downloadImFile(item.getRemoteMsgId(),
                item.getMessageInfo().messageType.value(), ImSendType.isP2P(item.getMessageInfo().imSendType) ? item.getMessageInfo().senderId : item.getMessageInfo().groupId,
                item.getMessageInfo().imSendType, imAudioMessage.msgContent, new ImCallback() {
                    @Override
                    public void callback(ImResponse imResponse) {
                        item.getMessageInfo().localPath = (String) imResponse.getData();
                        //为解决playAudioMessage 方法里面多次执行postValue的时候只执行最后一次需要改成setVlaue方法，这样的话之后在主线程调用，所以这里切为主线程
                        ImManager.getInstance().getMainHandler().post(() -> conversationViewModel.playAudioMessage(item));
                    }
                });
    }

}
